<?php

namespace plugin\agent\app\model;

use app\model\user\User;
use think\Model;

/**
 * t_agent_user 分销员表
 * @property integer $agent_id 主键
 * @property integer $user_id 用户id
 * @property integer $level_id 推广等级
 * @property integer $parent_agent_id 上级推广员id
 * @property integer $children_num 下级人员数
 * @property string $path 所有上级id
 * @property string $bind_time 绑定时间
 * @property integer $agent_order_num 提成单量
 * @property string $agent_order_amount 提成订单金额
 * @property string $agent_pay_amount 提成实际支付金额
 * @property string $total_bonus 总佣金数
 * @property string $draw_amount 已提款金额
 * @property integer $draw_num 已提款次数
 * @property string $residue_amount 剩余可提款金额
 * @property string $create_time 创建时间
 * @property string $update_time 更新时间
 */
class AgentUser extends Model
{
    /**
     * The connection name for the model.
     *
     * @var string|null
     */
    protected $connection = 'mysql';

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 't_agent_user';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $pk = 'agent_id';

    const STATUS_WAIT_CHECK = 0;     //待审核
    const STATUS_ALREADY_CHECK = 1;     //审核通过
    const STATUS_CHECK_ERROR = -1;    //审核失败
    const STATUS_DISABLE = -2;    //已禁用
    const STATUS_TEXT_MAP = [
        self::STATUS_WAIT_CHECK => "待审核",
        self::STATUS_ALREADY_CHECK => "审核通过",
        self::STATUS_CHECK_ERROR => "审核失败",
        self::STATUS_DISABLE => "已禁用",
    ];
    protected $append = [
        'status_text',
    ];

    public function getStatusTextAttr($value, $data)
    {
        return isset($data['status']) ? self::STATUS_TEXT_MAP[$data['status']] ?? "" : "";
    }


    public function user()
    {
        return $this->hasOne(User::class, 'id', "user_id");
    }

    public function level()
    {
        return $this->hasOne(AgentLevel::class, 'id', "level_id");
    }
}
